package com.yellowpage.white.exception;

/**
 * 异常基类
 * @author Administrator
 *
 */
public class BaseException extends Exception {

	private static final long serialVersionUID = -7339281910096541147L;

	
	/** 异常错误信息，由实际抛出异常的类定义 */
	private String errorMsg;

	/** 根异常，保持异常链 */
	private Throwable caused;

	public BaseException(String errorMsg) {
		super(errorMsg);

		this.errorMsg = errorMsg;
	}

	public BaseException(Throwable caused) {
		super(caused);

		this.caused = caused;
	}

	public BaseException(String errorMsg, Throwable caused) {
		super(errorMsg, caused);

		this.errorMsg = errorMsg;
		this.caused = caused;
	}

	/**
	 * 获得异常的错误信息
	 * 
	 * @return the errorMsg
	 */
	public String getErrorMsg() {
		/** 如果异常定义了错误信息 */
		if (errorMsg != null && !"".equals(errorMsg))
			return errorMsg;

		/**
		 * 如果没有定义错误信息,并且该异常是一个间接异常 则返回根异常的错误信息
		 */
		if (caused != null) {
			if (caused instanceof BaseException) {
				BaseException causedException = (BaseException) caused;
				return causedException.getErrorMsg();
			} else {
				return errorMsg;
			}
		}

		return errorMsg;
	}

	/**
	 * 设置异常的错误信息
	 * 
	 * @param errorMsg
	 *            the errorMsg to set
	 */
	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	/**
	 * 获得根异常
	 * 
	 * @return the caused
	 */
	public Throwable getCaused() {
		return caused;
	}

	/**
	 * 设置根异常
	 * 
	 * @param caused
	 *            the caused to set
	 */
	public void setCaused(Throwable caused) {
		this.caused = caused;
	}
}
